package com.yida.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.PageUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.system.api.domain.SysUser;
import com.yida.system.api.domain.SysUserCommunity;
import com.yida.system.api.domain.request.BindCommunityRequest;
import com.yida.system.api.domain.request.RegisterBindRequest;
import com.yida.system.api.domain.request.SysUserOutsideOfficerRemoveRequest;
import com.yida.system.api.domain.request.SysUserQueryRequest;
import com.yida.system.api.vo.OfficerUserVO;
import com.yida.system.service.ISysUserCommunityService;
import com.yida.system.service.ISysUserOutsideOfficerService;
import com.yida.system.service.ISysUserService;

import cn.hutool.core.util.ObjectUtil;

/**
 * @Author: PPV
 * @Date: 2023-03-08
 */

@Service
public class SysUserOutsideOfficerServiceImpl implements ISysUserOutsideOfficerService {

    @Resource
    private ISysUserService userService;

    @Resource
    private ISysUserCommunityService iSysUserCommunityService;

    @Override
    public List<OfficerUserVO> getPage(SysUserQueryRequest request) {
        PageUtils.startPage();
        return userService.selectOfficeUserList(request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindCommunity(BindCommunityRequest request) {
        userService.checkUserDataScope(request.getUserId());
        // 删除原有绑定
        iSysUserCommunityService.delUserAndCommunityByUserId(request.getUserId());
        // 新增绑定
        if (!iSysUserCommunityService.insertBatchUserAndCommunity(request.getUserId(), request.getInfos())) {
            throw new ServiceException("用户项目绑定失败");
        }
    }

    @Override
    public List<SysUserCommunity> getBindCommunities(Long userId) {
        return iSysUserCommunityService.getBindCommunityByUserId(userId);
    }

    @Override
    public void insertOustidUser(RegisterBindRequest request) {
        // 注册外网用户
        SysUser userInfo = request.getUserInfo();
        userInfo.setUserType("01");
        userInfo.setDeptId(102L);
        userInfo.setUserId(null);
        Long[] roleIds = {4L};
        userInfo.setRoleIds(roleIds);
        userInfo.setPassword(SecurityUtils.encryptPassword(userInfo.getPassword()));
        userService.insertUser(userInfo);
        // Long[] areaIds = {request.getUserInfo().getAreaId(), request.getUserInfo().getAreas().get(0).getAreaId(),
        // request.getBindCommunityRequest().getInfos().get(0).getStreetId()};
        Long[] areaIds = {request.getUserInfo().getAreas().get(0).getAreaId()};
        userService.insertUserBindArea(userInfo.getUserId(), areaIds);
        // 绑定用户项目
        if (!iSysUserCommunityService.insertBatchUserAndCommunity(userInfo.getUserId(), request.getBindCommunityRequest().getInfos())) {
            throw new ServiceException("用户项目绑定失败");
        }
    }

    /**
     * 删除用户
     * 
     * @param request 删除参数
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeOutsideUser(SysUserOutsideOfficerRemoveRequest request) {
        if (StringUtils.isNull(request.getUserId())) {
            throw new ServiceException("用户ID不能为空！");
        }
        Long userId = Long.valueOf(request.getUserId());
        SysUser sysUser = userService.selectUserById(userId);
        if (ObjectUtil.isEmpty(sysUser)) {
            throw new ServiceException("未查询到对应用户");
        }
        List<SysUserCommunity> bindCommunityByUserId = iSysUserCommunityService.getBindCommunityByUserId(userId);
        if (ObjectUtil.isEmpty(bindCommunityByUserId)) {
            throw new ServiceException("未查询到对应外网用户信息");
        }
        boolean contains = bindCommunityByUserId.stream().map(item -> item.getStreetId()).collect(Collectors.toList()).contains(Long.valueOf(request.getStreetId()));
        if (!contains) {
            throw new ServiceException("当前街道下无对应用户信息");
        }
        if (1 != userService.deleteUserById(userId)) {
            throw new ServiceException("用户删除失败");
        }
        if (!iSysUserCommunityService.delUserAndCommunityByUserId(userId)) {
            throw new ServiceException("用户及街道关联删除失败！");
        }
    }

}
